package com.example.wac.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wac.entity.Email;
import com.example.wac.mail.ToEmail;
import com.example.wac.mapper.EmailMapper;
import com.example.wac.service.EmailService;
import com.example.wac.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 邮件发送表 服务实现类
 * </p>
 *
 * @author wac
 * @since 2021-12-31
 */
@Slf4j
@Service("emailService")
/**
 * 单例controller多例service情况下通过设置多例模式并通过代理方式供controller注入
 */
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class EmailServiceImpl extends ServiceImpl<EmailMapper, Email> implements EmailService {

    @Autowired
    EmailMapper emailMapper;

    @Override
    public ApiResult findEmails() {
        Page<Email> page = new Page<>(1, 100);
        List<Email> emailList = emailMapper.selectList(page);
        return ApiResult.success(emailList);
    }

    @Override
    public ApiResult findEmailsByCondition(Map map) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        Integer id = Integer.valueOf((String) map.get("id"));
        Page<Email> page = new Page<>(1, 100);
        LambdaQueryChainWrapper<Email> wrapper = new LambdaQueryChainWrapper<>(this.baseMapper);
        if (id != null){
            wrapper.eq(Email::getId, id);
        }
        List<Email> records = wrapper.page(page).getRecords();
        return ApiResult.success(records);
    }

    @Override
    public void save(ToEmail toEmail) {
        Email email = new Email();
        email.setToEmail(toEmail.getTos());
        email.setFromEmail(toEmail.getFrom());
        email.setSubject(toEmail.getSubject());
        email.setContent(toEmail.getContent());
        log.info("保存邮件：[{}]", JSONObject.toJSONString(email));
    }

    @Override
    public ApiResult saveEmail(Email email) {
        this.save(email);
        return ApiResult.success(email);
    }

    @Override
    public ApiResult update(Email email) {
        LambdaUpdateChainWrapper<Email> wrapper = new LambdaUpdateChainWrapper<>(this.baseMapper)
                .eq(Email::getId,email.getId());
        wrapper.update(email);
        return ApiResult.success(email);
    }

    @Override
    public ApiResult delete(Integer id) {
        this.baseMapper.deleteById(id);
        return ApiResult.success();
    }
}
